package com.mq.mqserver.datacenter;

import com.mq.MqApplication;
import com.mq.mqserver.core.Binding;
import com.mq.mqserver.core.Exchange;
import com.mq.mqserver.core.ExchangeType;
import com.mq.mqserver.core.MSGQueue;
import com.mq.mqserver.Mapper.MetaMapper;

import java.io.File;
import java.util.List;

/*
* 通过这个类，整合上述的数据库操作*/
public class DataBaseManager {
    //从spring中拿不到现成对象
    private MetaMapper metaMapper;
    //数据库初始化：建库建表，以及一些默认数据
    //数据库/表有了，不做处理
    //数据库/表没有，创建表库，构造默认数据
    //例如将broker server部署到新的服务器上，自动建库建表
    //已经部署过的服务器上，broker server重启就会发现，数据库已有，不做处理
    //如何判断存在/不存在：meta.db这个文件是否存在
    public void init(){
        //手动获取metaMapper
        metaMapper = MqApplication.context.getBean(MetaMapper.class);
        if (metaMapper == null) {
            throw new IllegalStateException("MetaMapper 未正确初始化！");
        }
        if(!checkDBExists()){
            //创建data目录
            File dataDir = new File("./data");
            dataDir.mkdir();

            createTable();
            createDefaultData();
            System.out.println("[DatabaseManager] 数据库初始化完成！" );
        }else{
            System.out.println("[DatabaseManager] 数据库已存在！");
        }
    }
    private  boolean checkDBExists(){
        File file = new File("./data/meta.db");
        if(file.exists()){
            return true;
        }
        return false;
    }
    //建库不需要手动，只需要手动建表
    //建库操作不需要手动执行
    //首次执行这里的数据库操作时，会自动创建出meta.db文件来（Mybatis完成）
    private void createTable(){
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DatabaseManager] 建表完成！");
    }
    //主要添加默认的交换机
    //RabbitMQ里面有一个这样的设定：带有一个匿名的交换机，类型是 DIRECT
    private void createDefaultData(){
        //构造一个默认的交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DatabaseManager] 初始数据！");
    }
    //插入
    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }
    public void insertQueue(MSGQueue msgQueue){
        metaMapper.insertQueue(msgQueue);
    }
    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }
    //删除
    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }
    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }
    public void deleteBinding(Binding binding){
        metaMapper.deleteBinging(binding);
    }
    //查询
    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchanges();
    }
    public List<MSGQueue> selectAllQueues(){
        return metaMapper.selectAllQueues();
    }
    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }
    //删除meta.db
    public void deleteDB(){
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if(ret){
            System.out.println("删除数据库成功");
        }else{
            System.out.println("删除数据库失败");
        }
        File dataDir = new File("./data");
        //使用delete删除目录的时候，需要目录是空的
        ret = dataDir.delete();
        if(ret){
            System.out.println("删除数据库目录成功");
        }else{
            System.out.println("删除数据库目录失败");
        }
    }


}
